package com.baizhi.cmfzdemo.controller;

import com.baizhi.cmfzdemo.entity.CmfzBanner;
import com.baizhi.cmfzdemo.service.CmfzBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by 54433 on 2019/4/4.
 */
@Controller
@RequestMapping("/banner")
public class CmfzBannerController {
    @Autowired
    private CmfzBannerService bannerService;

    //展示所有
    @RequestMapping("/queryAllBanner")
    @ResponseBody
    public Map queryAllBanner(int page,int rows){
        System.out.println(bannerService.queryAllBanner(page,rows));
        return bannerService.queryAllBanner(page,rows);
    }
    @RequestMapping("/deleteBanner")
    @ResponseBody
    public Map deleteBanner(int [] ids){
        System.out.println(Arrays.toString(ids));
        return bannerService.deleteBanner(ids);
    }
    @RequestMapping("/insertBanner")
    @ResponseBody
    public Map insertBanner(CmfzBanner cmfzBanner,MultipartFile picBanner, HttpServletRequest request) throws IOException {
        //获取到原始文件名；上传上来的那个文件的名字
        System.out.println(cmfzBanner);
        System.out.println(picBanner);
        String fileName = picBanner.getOriginalFilename();

        cmfzBanner.setBannerImageUrl("/img/"+fileName);
/*获取到upload的绝对路径*/
        String realPathNewMethod = request.getServletContext().getRealPath("img");
        picBanner.transferTo(new File(realPathNewMethod+"/"+fileName));
        return bannerService.addBanner(cmfzBanner);
    }
    @RequestMapping("/updateBanner")
    @ResponseBody
    public Map updateBanenr(CmfzBanner cmfzBanner,MultipartFile picb,HttpServletRequest request) throws IOException {
        //获取到原始文件名；上传上来的那个文件的名字
        System.out.println(cmfzBanner);
        System.out.println(picb);
        String fileName = picb.getOriginalFilename();
        cmfzBanner.setBannerImageUrl("/img/"+fileName);
        /*获取到upload的绝对路径*/
        String realPathNewMethod = request.getServletContext().getRealPath("img");
        picb.transferTo(new File(realPathNewMethod+"/"+fileName));
        return bannerService.updateBanner(cmfzBanner);
    }

}
